$(document).ready(function(){
	var doctor_id="";
	
	$('#okbtn').click(function(){
		var name=$('#name').val();
		var jumin1 = $('#jumin1').val();
		var jumin2 = $('#jumin2').val();
		var password =$('#password').val();
		var addr=$('#addr').val();
		var gender = $('#gender').val();
		var tel = $('#tel').val();
		var license = $('#license').val();
		var file = $('.file').val();
		//alert(file);
		var greeting=$('#greeting').val();
		$.ajax({
			url : "doctor_insert.do",
			data : "name="+name+"&jumin1="+jumin1+"&jumin2="+jumin2+
			"&password="+password+"&addr="+addr+"&gender="+gender+
			"&tel="+tel+"&license="+license+"&photo="+file+"&greeting="+greeting,
			dataType : 'json',
			success : function(data){

				$("#div1").hide();
				$("#div2").fadeIn(1300);
				var html="<option>------과 선택------</option>";
				$('#dept_name').html(html);
				doctor_id=data.doctor.doctor_id;
				for(var i =0 ; i< data.dept.length;i++){
				var dept = "<option id='dept_id' value='"+data.dept[i].dept_id+"'>"+data.dept[i].deptname+"</option>";
				$('#dept_name').append(dept);
				}	
			}
		});
		
	})
	
	$('#dept_name').change(function(){
		var dept_id=$('#dept_name').val()
		$.ajax({
			url:"deptdetail.do",
			data : "dept_id="+dept_id,
			dataType : "json",
			success : function (data) {
				var html="<option>------주 전공------</option>";
				$("#dept_detail_name").html(html);
				for(var i =0; i<data.dept.length;i++){
					var depts="<option id='dept_detail_id' value='"+data.dept[i].dept_detail_id+"'>"+data.dept[i].detail_name+"</option>";
					$("#dept_detail_name").append(depts);
				}
			}
		})
	})
	
	
	$('#deptchoice').click(function(){
		var dept_detail_id=$('#dept_detail_name').val();
		$.ajax({
			url:"deptdetail2.do",
			data : "dept_id="+dept_id+"&dept_detail_id="+dept_detail_id+
			"&doctor_id="+doctor_id,
			dataType : "json",
			success : function (data) {
				if(data.yes>0){
					alert('추가 되었습니다');
				}
			},
			error : function(data){
				alert("이미 추가되어 있습니다")
			}
		})
	})
		
	$('#deptokbtn').click(function(){
		$('#div2').hide();
		$('#div3').fadeIn(1300);
	})
	
	
	$( ".datepicker" ).datepicker({
		dateFormat: 'yy-mm-dd' 
	});
	
	$('#finalbtn').click(function(){
		var start_year = $('#start_year').val();
		var end_year = $('#end_year').val();
		var content = $('#content').val();
		$.ajax({
			url : "doctorinsertfinal.do",
			data : "start_year="+start_year+"&end_year="+end_year+
			"&content="+content+"&doctor_id="+doctor_id,
			dataType: "json",
			success : function(data){
				if(data.yes>0){
					alert("의사 등록이 완료 되었습니다");
					location.href="doctorinsert.do";
					
				}else{
					alert("의사 등록 실패 ");
					location.href="doctorinsert.do";
				}
				
			},
			error : function(data){
				alert("의사 등록 실패");
				location.href="doctorinsert.do";
			}
			
		})
		
		
		
	})
	
	
	
})